package com.hk.baihua.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.hk.common.action.BaseSupport;
import com.hk.common.util.PageInfo;
import com.hk.baihua.entity.CjBean;
import com.hk.baihua.service.CjBeanService;
import com.hk.util.ControlUtil;
import com.hk.util.JsonFilter;
import com.hk.util.JsonModel;

/**
 *
 * @author  
 *
 */
@ParentPackage("json-default")
@Namespace("/baihua/cjBean")
@Results({
    @Result(name="listCjBean",location="/WEB-INF/page/baihua/cjBean/listCjBean.jsp"),
    @Result(name="cjBeanEntity",type="json"),
    @Result(name="jm",type="json",params={"contentType","text/html"}),
    @Result(name="editCjBean",location="/WEB-INF/page/baihua/cjBean/editCjBean.jsp"),
    @Result(name="detailCjBean",location="/WEB-INF/page/baihua/cjBean/detailCjBean.jsp")
})
public class CjBeanAction extends BaseSupport<CjBean>{

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private static final String LIST_PAGE = "listCjBean";
    private static final String DETAIL_PAGE = "editCjBean";
    private static final String DETAIL = "detailCjBean";

    protected JsonModel jm = new JsonModel();
    private CjBean cjBean;		//单个cjBean记录

    @Autowired
    private CjBeanService cjBeanService;

    @Action("/listCjBean")
    public String list() throws Exception{
        return LIST_PAGE;
    }

    @Action("/listCjBeanData")
    public String listData(){
        try {
            HttpServletResponse response = ServletActionContext.getResponse();
            response.setContentType("text/html;charset=UTF-8");
            JSONObject jsonobj = new JSONObject();
            PrintWriter out = response.getWriter();
            Map<String,Object> map = new HashMap<String,Object>();
            //如果有自定义排序字段，在该注释下面写

            //自定义排序字段设置结束
            map = ControlUtil.getParams(ServletActionContext.getRequest(),map);
            PageInfo<CjBean> pageInfo = ControlUtil.getPageinfo(ServletActionContext.getRequest());
            pageInfo.setResult(cjBeanService.findListByMap(null, map, pageInfo));
            pageInfo.setCount(cjBeanService.countByPage(map));
            JSONArray rows = null;
            long total = 0L;
            if(null != pageInfo && null != pageInfo.getResult() && pageInfo.getResult().size() > 0){
                rows = JSONArray.fromObject(pageInfo.getResult(),JsonFilter.getFilter());
                total = pageInfo.getCount();
            } else {
                rows = JSONArray.fromObject(new ArrayList<CjBean>(),JsonFilter.getFilter());
                total = 0;
            }
            Map<String,Object> json = new HashMap<String, Object>();
            json.put("rows", rows);
            json.put("total", total);
            jsonobj = JSONObject.fromObject(json);// 格式化result一定要是JSONObject
            out.print(jsonobj);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Action("/getCjBean")
    public String getCjBeanEntity() throws Exception{
        cjBean = cjBeanService.get(id);
        if(null == cjBean) cjBean = new CjBean();
        return "cjBeanEntity";
    }

    @Action("/editCjBean")
    public String edit() throws Exception{
        cjBean = cjBeanService.get(id);
        if(null == cjBean)
            cjBean = new CjBean();
        this.setCjBean(cjBean);
        return DETAIL_PAGE;
    }

    @Action("/updateCjBean")
    public String update() throws Exception{
        cjBeanService.update(cjBean);
        return LIST_PAGE;
    }

    @Action("/saveCjBean")
    public String saveOrUpdate() throws Exception{
        try{
            cjBeanService.saveOrUpdate(cjBean);
            jm.setMsg("保存成功!");
        } catch (Exception e) {
            jm.setMsg("保存失败!");
            jm.setFlag(true);
        }
        return "jm";
    }

    @Action("/delCjBeans")
    public String deleteMulti() throws Exception {
        try{
            cjBeanService.removeAll(ids.split(","));
            jm.setMsg("删除成功 !");
        } catch (Exception e) {
            jm.setMsg("删除失败!");
            jm.setFlag(true);
        }
        return "jm";
    }
    
    @Action("/detailCjBean")
    public String detailCjBean(){
    	cjBean = cjBeanService.get(id);
    	if(null != cjBean)
    		this.setCjBean(cjBean);
    	return DETAIL;
    }

    public CjBean getCjBean(){
        return this.cjBean;
    }
    public void setCjBean(CjBean cjBean){
        this.cjBean = cjBean;
    }

    public JsonModel getJm() {
        return jm;
    }
    public void setJm(JsonModel jm) {
        this.jm = jm;
    }

}
